<?php
class Dispatcher {

	public function dispatch(Request $request, Router $router) {
		$router->parseUrl($request->getUrl());
		$router->checkPermission();
		$controller = $this->_invokeController($router->getController());
		$controller->invokeAction($router->getAction(), $router->getParams(), $request->getForm(), $request->getFiles());
			
	}

	private function _invokeController($controller) {
		if(class_exists($controller)) {
			return new $controller;
		} else {
			header(DEFAULT_LOCATION);
			exit;
		}
		
	}

}